from django.contrib.auth.models import AbstractUser
from django.db import models

from utils.models import (
    IsDeletedBaseModel, TimeBaseModel, MODEL_KWARGS, FMODEL_KWARGS, TreeModel
)


class User(AbstractUser, IsDeletedBaseModel, TimeBaseModel):
    name = models.CharField(max_length=118, **MODEL_KWARGS)
    tel = models.CharField(max_length=11, **MODEL_KWARGS)

    class Meta:
        db_table = 'system_user'
        verbose_name = '用户表'


class Role(IsDeletedBaseModel, TimeBaseModel):
    name = models.CharField(max_length=118, **MODEL_KWARGS)
    order_num = models.IntegerField(default=0, verbose_name='排序字眼', **MODEL_KWARGS)

    class Meta:
        db_table = 'system_role'
        verbose_name = '角色表'


class UserRole(IsDeletedBaseModel):
    user = models.ForeignKey(User, related_name='user_role', **FMODEL_KWARGS)
    role = models.ForeignKey(Role, related_name='user_role', **FMODEL_KWARGS)

    class Meta:
        db_table = 'system_user_role'
        verbose_name = '用户角色表'


class Resource(TreeModel, IsDeletedBaseModel, TimeBaseModel):
    name = models.CharField(max_length=118, **MODEL_KWARGS)
    order_num = models.IntegerField(default=0, verbose_name='排序字眼', **MODEL_KWARGS)
    type = models.SmallIntegerField(verbose_name='0 菜单 1 请求方式')
    method = models.SmallIntegerField(verbose_name='请求方式0 get 1 post 2 put 3 delete', **MODEL_KWARGS)
    url = models.CharField(max_length=118, verbose_name='url', **MODEL_KWARGS)

    class Meta:
        db_table = 'system_resource'
        verbose_name = '资源表'


class RoleResource(IsDeletedBaseModel):
    resource = models.ForeignKey(Resource, related_name='role_resource', **FMODEL_KWARGS)
    role = models.ForeignKey(Role, related_name='role_resource', **FMODEL_KWARGS)

    class Meta:
        db_table = 'system_role_resource'
        verbose_name = '角色资源表'


class Url(models.Model):
    name = models.CharField(max_length=118, **MODEL_KWARGS)

    class Meta:
        db_table = 'system_url'
        verbose_name = 'url表'
